#!/bin/sh /etc/rc.common
#copyright by sirpdboy

START=99
STOP=10
TMP=/etc/taskplan
LOG=$TMP/taskplan.log
TT=/etc/taskplan/taskplanrun
CR=/etc/crontabs/root

# 确保目录存在
[ ! -d $TMP ] && mkdir -p $TMP 2>/dev/null
[ ! -s $LOG ] && echo '' > $LOG

run_taskplan()
{
    [  `uci -q get taskplan.@global[0].customscript  |  wc -l ` > 0 ] && uci -q get taskplan.@global[0].customscript > /etc/taskplan/taskplancustomscript && sed -i 's/\r$//' /etc/taskplan/taskplancustomscript
    [  `uci -q get taskplan.@global[0].customscript2  |  wc -l ` > 0 ] && uci -q get taskplan.@global[0].customscript2 > /etc/taskplan/taskplancustomscript2 && sed -i 's/\r$//' /etc/taskplan/taskplancustomscript2
    
    ssum=$(grep -c stime /etc/config/taskplan)
    lsum=$(grep -c ltime /etc/config/taskplan)
    
    for i in $(seq 0 $((ssum-1)))
    do
        enable=$(uci -q get taskplan.@stime[$i].enable )
        if [ "x$enable" = "x1" ]; then
            month=$(uci -q get taskplan.@stime[$i].month ) || month="*"
            stype=$(uci -q get taskplan.@stime[$i].stype )
            week=$(uci -q get taskplan.@stime[$i].week ) || week="*"
            minute=$(uci -q get taskplan.@stime[$i].minute ) || minute="00"
            hour=$(uci -q get taskplan.@stime[$i].hour )  || hour="*"
            [ "x$hour" = "x0" ] && hour="00"
            [ "x$minute" = "x0" ] && minute="00"
            
            case "$stype" in
                1) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler reboot Scheduled_task" ;;
                2) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler poweroff Scheduled_task" ;;
                3) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler network Scheduled_task" ;;
                4) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartsamba Scheduled_task" ;;
                5) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartwan Scheduled_task" ;;
                6) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler closewan Scheduled_task" ;;
                7) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler clearmem Scheduled_task" ;;
                8) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler sysfree Scheduled_task" ;;
                9) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler disreconn Scheduled_task" ;;
                10) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler disrereboot Scheduled_task" ;;
                11) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartmwan3 Scheduled_task" ;;
                12) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler customscript Scheduled_task" ;;
                13) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler upwifi Scheduled_task" ;;
                14) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler downwifi Scheduled_task" ;;
                15) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler customscript2 Scheduled_task" ;;
                16) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartlan Scheduled_task" ;;
            esac
            echo "$cmd" >> $CR
        fi
    done

    # 清空并重新创建 taskplanrun 文件
    echo "#!/bin/sh" > $TT
    chmod +x $TT
    
    for i in $(seq 0 $((lsum-1)))
    do
        enable=$(uci -q get taskplan.@ltime[$i].enable )
        if [ "x$enable" = "x1" ]; then
            stype=$(uci -q get taskplan.@ltime[$i].stype )
            delay=$(uci -q get taskplan.@ltime[$i].delay )  || delay=10
            
            case "$stype" in
                1) echo  "(sleep $delay && /usr/bin/taskplanhandler reboot Startup_task) &" >>$TT ;;
                2) echo  "(sleep $delay && /usr/bin/taskplanhandler poweroff Startup_task) &" >>$TT ;;
                3) echo  "(sleep $delay && /usr/bin/taskplanhandler network Startup_task) &" >>$TT ;;
                4) echo  "(sleep $delay && /usr/bin/taskplanhandler restartsamba Startup_task) &" >>$TT ;;
                5) echo  "(sleep $delay && /usr/bin/taskplanhandler restartwan Startup_task) &" >>$TT ;;
                6) echo  "(sleep $delay && /usr/bin/taskplanhandler closewan Startup_task) &" >>$TT ;;
                7) echo  "(sleep $delay && /usr/bin/taskplanhandler clearmem Startup_task) &" >>$TT ;;
                8) echo  "(sleep $delay && /usr/bin/taskplanhandler sysfree Startup_task) &" >>$TT ;;
                9) echo  "(sleep $delay && /usr/bin/taskplanhandler disreconn Startup_task) &" >>$TT ;;
                10) echo  "(sleep $delay && /usr/bin/taskplanhandler disrereboot Startup_task) &" >>$TT ;;
                11) echo  "(sleep $delay && /usr/bin/taskplanhandler restartmwan3 Startup_task) &" >>$TT ;;
                12) echo  "(sleep $delay && /usr/bin/taskplanhandler customscript Startup_task) &" >>$TT ;;
                13) echo  "(sleep $delay && /usr/bin/taskplanhandler upwifi Startup_task) &" >>$TT ;;
                14) echo  "(sleep $delay && /usr/bin/taskplanhandler downwifi Startup_task) &" >>$TT ;;
                15) echo  "(sleep $delay && /usr/bin/taskplanhandler customscript2 Startup_task) &" >>$TT ;;
                16) echo  "(sleep $delay && /usr/bin/taskplanhandler restartlan Startup_task) &" >>$TT ;;
            esac
        fi
    done
    
    echo 'wait' >> $TT  # 等待所有后台任务完成
    echo 'exit 0' >> $TT
}

start()
{
    del_cru
    run_taskplan
    /etc/init.d/cron reload
    
    # 记录启动日志
    echo "$(date): taskplan started" >> $LOG
}

boot()
{
    # 在启动时执行任务
    del_cru
    run_taskplan
    /etc/init.d/cron reload

    # 确保 taskplanrun 文件存在并执行
    if [ -f $TT ] && [ -s $TT ]; then
        echo "$(date): Executing taskplanrun at boot" >> $LOG
        bash $TT >> $LOG 2>&1 &
    else
        echo "$(date): taskplanrun file not found or empty at boot" >> $LOG
    fi
}

stop()
{
    del_cru
    /etc/init.d/cron reload
    echo "$(date): taskplan stopped" >> $LOG
}

restart()
{
    stop
    sleep 2
    start
}

del_cru()
{   
    sed -i '/taskplanhandler/d' $CR >/dev/null 2>&1 || true
    echo "#!/bin/sh" > $TT  # 清空文件但保留基本结构
    chmod +x $TT
}
